package com.nageoffer.subject.domain.service.impl;

import com.nageoffer.subject.common.enums.IsDeletedFlagEnums;
import com.nageoffer.subject.domain.convert.SubjectLabelBOConverter;
import com.nageoffer.subject.domain.entity.SubjectLabelBO;
import com.nageoffer.subject.domain.service.SubjectLabelDomainService;
import com.nageoffer.subject.infra.basic.entity.SubjectLabel;
import com.nageoffer.subject.infra.basic.entity.SubjectMapping;
import com.nageoffer.subject.infra.basic.service.SubjectLabelService;
import com.nageoffer.subject.infra.basic.service.SubjectMappingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签接口实现层
 */
@Service
@RequiredArgsConstructor
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    private final SubjectLabelService subjectLabelService;

    private final SubjectMappingService subjectMappingService;

    @Override
    public Boolean addLabel(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        int result = subjectLabelService.insert(subjectLabel);
        return result > 0;
    }

    @Override
    public Boolean updateLabel(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        int result = subjectLabelService.update(subjectLabel);
        return result > 0;
    }

    @Override
    public Boolean deleteLabel(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnums.DELETED.getCode());
        int result = subjectLabelService.update(subjectLabel);
        return result > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnums.UN_DELETED.getCode());
        // 查询分类下面的所有标签 ID
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = subjectMappingList.stream()
                .map(SubjectMapping::getSubjectId)
                .collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> labelBOList = new ArrayList<>();
        labelList.forEach(each -> {
            SubjectLabelBO labelBO = new SubjectLabelBO();
            labelBO.setId(each.getId());
            labelBO.setLabelName(each.getLabelName());
            labelBO.setCategoryId(categoryId);
            labelBOList.add(labelBO);
        });
        return labelBOList;
    }
}
